Writing XML with the XmlWriter class in C#

In the previous section we have read XML, but now it's time to start typing. Since XML is just text, you can just start typing XML tags in a file and give it XML extension, but NAT framework is easier and safer to handle it, and just like reading XML, At least two different options: XMLRRIR Approach and XmlDocument Approach This article will focus on the first approach, and after that we will use XML in the next chapter Write XML with Itail.

The difference between the two is often related to most of the memory consumption - XMLRM uses less memory compared to XMLDCast, which is a problem if you write a large file. Another important difference is that while using XmlDocument, you can read an existing file, manipulate it, and then write back the changes. With XmlWriter, you have to write the whole document every time from scratch. It is not necessarily a problem, as usual, it actually comes under your personal preferences along with your situation.

Here's an example of XML writing using the XmlWriter class:


using System;
using System.Text;
using System.Xml;

namespace WritingXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlWriter xmlWriter = XmlWriter.Create("test.xml");

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("users");

            xmlWriter.WriteStartElement("user");
            xmlWriter.WriteAttributeString("age", "42");
            xmlWriter.WriteString("John Doe");
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("user");
            xmlWriter.WriteAttributeString("age", "39");
            xmlWriter.WriteString("Jane Doe");

            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
        }
    }
}

The above code will create the following XML:


<users>
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
</users>

Therefore, we start by making an example of XmlWriter class. It contains at least one parameter, in this case where XML should be written, but comes in many forms for different purposes. First of all we have to do that call the WriteStartDocument () method. After that, we write the initial element called "user". XMLRiator will translate it into <users> Before turning it off, we write another initial element, "user", which will then become the child of "user". After this, we add an attribute (age) to the element by using the WriteAttributeString () method, and then we write the WriteString () method and write the interior part of the element. We then make sure to close the "user" element with the call to the WriteEndElement () method.

This process is repeated for adding another user, except that we do not call WriteEndElement () as we did earlier in fact, it should be said twice, because we also have an open "user" element , But XMLWriter will do this for us when we call the WriteEndDocument () method.

To write XmlWriter to your data on the disc, we call the off () method. Now you can open the file "test.xml", in the same directory where the EXE file of your project is usually in the bin \ debug directory.

And it actually takes a simple XML file to write. In the next chapter, we will do this, but using the XmlDocument class.